<template>
  <div class="app-container">
    <div class="filter-container">
      <!--查询表单-->
      <el-form :inline="true" class="demo-form-inline" size="mini">

        <el-form-item>
          <el-select
            v-model="dataQuery.categoryId"
            filterable
            placeholder="请选择类别">
            <el-option
              v-for="category in categoryList"
              :key="category.id"
              :label="category.name"
              :value="category.id"/>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-input v-model="dataQuery.title" placeholder="标题"/>
        </el-form-item>

        <el-form-item label="添加时间">
          <el-date-picker
            v-model="dataQuery.begin"
            type="datetime"
            placeholder="选择开始时间"
            value-format="yyyy-MM-dd HH:mm:ss"
            default-time="00:00:00"
          />
        </el-form-item>

        <el-form-item>
          <el-date-picker
            v-model="dataQuery.end"
            type="datetime"
            placeholder="选择截止时间"
            value-format="yyyy-MM-dd HH:mm:ss"
            default-time="00:00:00"
          />
        </el-form-item>

        <el-button type="primary" icon="el-icon-search" size="mini" @click="getList()">查询</el-button>
        <el-button type="default" icon="el-icon-refresh" size="mini" @click="resetData()">清空</el-button>
        <el-button type="primary" icon="el-icon-circle-plus-outline" size="mini" @click="goEdit()"
                   v-permission="'path:add'">添加
        </el-button>

      </el-form>
    </div>
    <el-table :data="list" v-loading="listLoading" border fit
              highlight-current-row>
      <el-table-column align="center" label="序号" width="80">
        <template slot-scope="scope">
          {{ (page - 1) * limit + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="分类">
        <template slot-scope="scope">
          {{ getCategoryName(scope.row.categoryId) }}
        </template>
      </el-table-column>

      <el-table-column label="路径" width="380">
        <template slot-scope="scope">
          <div class="d-flex">
            <img :src="scope.row.cover" class="cover-image" alt="封面">
            <span class="ml10">{{scope.row.name}}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="价格" width="120">
        <template slot-scope="scope">
          <div>现价：{{scope.row.price}}元</div>
          <div class="original-charge">原价：{{scope.row.originalPrice}}元</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="步骤">
        <template slot-scope="scope">
          <span>{{scope.row.stepsCount||0}}个步骤</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="课程">
        <template slot-scope="scope">
          <span>{{scope.row.coursesCount||0}}个课程</span>
        </template>
      </el-table-column>

      <el-table-column align="center" prop="gmtCreate" label="创建时间" width="170"/>
      <el-table-column align="center" prop="gmtModified" label="最近修改时间" width="170"/>
      <el-table-column align="center" label="管理" width="200" fixed="right">
        <template slot-scope="scope">
          <el-link type="primary" @click="goStep(scope.row.id)" v-permission="'path:update'">步骤</el-link>
          <el-link type="primary" @click="goEdit(scope.row.id)" v-permission="'path:update'">编辑</el-link>
          <el-link type="danger" @click="removeDataById(scope.row.id)" v-permission="'path:delete'">删除</el-link>

        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :current-page="page"
      :page-size="limit"
      :total="total"
      background
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="getList"
    />
  </div>
</template>
<script>
  export default {
    data() {
      return {
        dataQuery: {},
        list: [],//表格的数据
        categoryList: [],
        categoryMap:  new Map(),
        listLoading: false,//数据加载等待动画
        page: 1, //当前页
        limit: 5, //每页记录数
        total: 0, //总记录数
      }
    }, computed: {
      getCategoryName() {
        return (categoryId) => {
          return this.categoryMap.get(categoryId)
        }
      }
    }, created() {
      this.getCategoryList()
      this.getList()
    }
    , methods: {
      getCategoryList() {
        this.api({
          url: `/path/getCategoryList`,
          method: 'get',
        }).then(response => {
          //删除视频后重置id
          this.categoryList = response
          // this.categoryMap = new Map();
          this.categoryList.forEach((item) => {
            this.categoryMap.set(item.id, item.name)
          })
        })
      },
      getList() {
        this.api({
          url: `/path/pageListPathCondition/${this.page}/${this.limit}`,
          method: 'post',
          data: this.dataQuery
        }).then(data => {
          //删除视频后重置id
          this.list = data.list;
          this.total = data.totalCount;
        })
      },
      resetData(){
        this.dataQuery={}
        this.getList()
      },
      goEdit(id) {
        let s = id ? '/' + id : '';
        this.$router.push('/learningpath/edit' + s)
      },
      goStep(id) {
        this.$router.push("/learningpath/step/" + id)
      },
      removeDataById(id){
        this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return this.api({
            url: `/path/delPath/${id}`,
            method: "get",
          })
        }).then(() => {
          this.getList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch((response) => { // 失败
          if (response === 'cancel') {
            this.$message({
              type: 'info',
              message: '已取消删除'
            })
          } else {
            this.$message({
              type: 'error',
              message: '删除失败'
            })
          }
        })
      }
    }
  }
</script>
<style scoped>
  .cover-image {
    width: 120px;
    height: 90px;
  }


  .ml10 {
    margin-left: 10px;
  }

  .original-charge {
    text-decoration: line-through;
  }
</style>
